package com.aqielife.hk.service.impl;

import com.aqielife.hk.common.OsSelect;
import com.aqielife.hk.dto.LoginDTO;
import com.aqielife.hk.sdk.FMSGCallBack_V31;
import com.aqielife.hk.sdk.HCNetSDK;
import com.aqielife.hk.service.IHcnetService;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author aqie
 * @Date 2022/4/19 11:21
 * @desc
 */
@Service
public class HcnetServiceImpl implements IHcnetService {

    @Autowired
    private HCNetSDK hCNetSDK;

    static int lUserID = -1;//用户句柄
    public static int lAlarmHandle = -1;
    public static int lListenHandle = -1;

    static int iCharEncodeType = 0;//设备字符集



    @Override
    public int login(LoginDTO login) {
        //注册
        HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();//设备登录信息
        String m_sDeviceIP = login.getIp();//设备ip地址
        m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
        System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
        String m_sUsername = login.getName();//设备用户名
        m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
        System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
        String m_sPassword = login.getPassword();//设备密码
        m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
        System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
        m_strLoginInfo.wPort = login.getPort(); //sdk端口
        m_strLoginInfo.bUseAsynLogin = false; //是否异步登录：0- 否，1- 是
        m_strLoginInfo.write();
        HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息
        lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
        if (lUserID == -1) {
            System.out.println("登录失败，错误码为" + hCNetSDK.NET_DVR_GetLastError());
            return lUserID;
        } else {
            System.out.println("登录成功！");
            m_strDeviceInfo.read();
            iCharEncodeType = m_strDeviceInfo.byCharEncodeType;
            return lUserID;
        }
    }

    @Override
    public void logout(int lUserID) {
        /**退出之前判断布防监听状态，并做撤防和停止监听操作*/
        if (lAlarmHandle >= 0){
            if (!hCNetSDK.NET_DVR_CloseAlarmChan_V30(lAlarmHandle)){
                System.out.println("撤防失败，错误码：" + hCNetSDK.NET_DVR_GetLastError());
            }else{
                System.out.println("撤防成功！！！");
            }
        }
        if (lListenHandle >= 0){
            if (!hCNetSDK.NET_DVR_StopListen_V30(lListenHandle)){
                System.out.println("取消监听失败，错误码：" + hCNetSDK.NET_DVR_GetLastError());
            }else{
                System.out.println("停止监听成功！！！");
            }
        }

        /**登出和清理，释放SDK资源*/
        if (lUserID>=0)
        {
            hCNetSDK.NET_DVR_Logout(lUserID);
        }
        hCNetSDK.NET_DVR_Cleanup();
    }

    @Override
    public void setAlarm(int userID) {
        if (lAlarmHandle < 0)//尚未布防,需要布防
        {
            //报警布防参数设置
            HCNetSDK.NET_DVR_SETUPALARM_PARAM m_strAlarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
            m_strAlarmInfo.dwSize = m_strAlarmInfo.size();
            m_strAlarmInfo.byLevel = 1;  //布防等级
            m_strAlarmInfo.byAlarmInfoType = 1;   // 智能交通报警信息上传类型：0- 老报警信息（NET_DVR_PLATE_RESULT），1- 新报警信息(NET_ITS_PLATE_RESULT)
            m_strAlarmInfo.byDeployType = 0;   //布防类型 0：客户端布防 1：实时布防
            m_strAlarmInfo.write();
            lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(userID, m_strAlarmInfo);
            System.out.println("lAlarmHandle: " + lAlarmHandle);
            if (lAlarmHandle == -1) {
                System.out.println("布防失败，错误码为" + hCNetSDK.NET_DVR_GetLastError());
            } else {
                System.out.println("布防成功");
            }
        }
    }
}
